﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;

namespace BioCompEx3
{
    //Holds the content of the files as Tupples stored in learning, validation and testing queues
    class DB
    {
        private Queue<Tupple> learning; //'learnPercentage'% of the tupples from 'data' file
        private Queue<Tupple> validation;   //(100-'learnPercentage')% of the tupples from 'data' file
        private Queue<Tupple> testing;  //all tupples of 'input' file
        private StreamReader data;
        private StreamReader input;
        private int learnPercentage;
        public long lAmmountOfValidationTupples;

        //CTOR - inits the class' variables, reads the data file into tupples and splits them to learning and validation,
        //reads the data file into tupples
        public DB(string dataFileName, string inputFileName, int learnPercentage)
        {
            learning = new Queue<Tupple>();
            validation = new Queue<Tupple>();
            testing = new Queue<Tupple>();
            data = new StreamReader(dataFileName);
            input = new StreamReader(inputFileName);
            this.learnPercentage = learnPercentage;
            readDataFile();
            readInputFile();
        }

        //reads data file into tupples and splits then to learning and validation
        private void readDataFile()
        {
            String line = data.ReadLine();
            while (line != null)
            {
                if (!line.Trim().Equals(""))    //EOF
                    validation.Enqueue(new Tupple(line, false));
                line = data.ReadLine();
            }
            data.Close();
            splitLearningValidation();
        }

        //splits first tupples into learning until reaching the learning percentage and the rest remains as validation
        private void splitLearningValidation()
        {
            int rowsCount = validation.Count;
            int learnCount = rowsCount * learnPercentage / 100;
            for (int i = 0; i < learnCount; i++)
            {
                learning.Enqueue(validation.Dequeue());
            }
            lAmmountOfValidationTupples = rowsCount - learnCount;
        }

        //reads all input file content into tupples
        private void readInputFile()
        {
            String line = input.ReadLine();
            while (line != null)
            {
                if (!line.Trim().Equals(""))
                    testing.Enqueue(new Tupple(line, true));
                line = input.ReadLine();
            }
            input.Close();
        }

        //returns and removes the first tupple of the learning queue
        public Tupple getNextLearningTupple()
        {
            if (learning.Count == 0)
                return null;
            return learning.Dequeue();
        }

        //returns and removes the first tupple of the validation queue
        public Tupple getNextValidationTupple()
        {
            if (validation.Count == 0)
                return null;
            return validation.Dequeue();
        }

        //returns and removes the first tupple of the test queue
        public Tupple getNextTestTupple()
        {
            if (testing.Count == 0)
                return null;
            return testing.Dequeue();
        }
        
    }
}
